package View;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.*;

import Model.*;
import Controller.*;

import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;

public class TelaEstoque extends JPanel{
	
	JPanel estoque, utility, conteudo, categorias;
	JButton categoria, Item, editar, excluir;
	JScrollPane scrollPane;
	private JTable table;
	JTree tree;
	
	TelaCategoriaEstoque tce;
	TelaItemEstoque tie;
	private JButton btnSubcategoria;
	static GerenciadorAplicacoes gapp;
	JTextField tid, tnome;
	JPanel panel;
	DefaultMutableTreeNode raiz = new DefaultMutableTreeNode("RAIZ");
	DefaultMutableTreeNode cat;
	DefaultMutableTreeNode subcategoria = new DefaultMutableTreeNode();
	
	public TelaEstoque()  {
		
		utility = new JPanel();
		utility.setBackground(new Color(0, 175, 239));
		utility.add(categoria = new JButton("CATEGORIA"));
		categoria.addActionListener(new ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				panel.setVisible(false);
				tce = new TelaCategoriaEstoque();
				conteudo.removeAll();
				conteudo.add(tce.categoria);
				tce.categoria.setVisible(true);
				conteudo.validate();
			}});
		
		categoria.setBackground(new Color(255, 255, 255));
		categoria.setForeground(new Color(0, 175, 239));
		utility.add(Item = new JButton("ITEM"));
		
		Item.addActionListener(new ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				panel.setVisible(false);
				tie = new TelaItemEstoque();
				conteudo.removeAll();
				conteudo.add(tie.item);
				tie.item.setVisible(true);
				conteudo.validate();
			}});
		Item.setBackground(new Color(255, 255, 255));
		Item.setForeground(new Color(0, 175, 239));
		conteudo = new JPanel();
		conteudo.setBackground(new Color(0, 175, 239));
		
		JScrollPane scrollPane_1 = new JScrollPane();
		
		excluir = new JButton("EXCLUIR");
		excluir.setBackground(new Color(255, 255, 255));
		excluir.setForeground(new Color(0, 175, 239));
		
		editar = new JButton("EDITAR");
		editar.setBackground(new Color(255, 255, 255));
		editar.setForeground(new Color(0, 175, 239));
		editar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				panel.setVisible(false);
			}
		});
		
		btnSubcategoria = new JButton("SUB-CATEGORIA");
		btnSubcategoria.setBackground(new Color(255, 255, 255));
		btnSubcategoria.setForeground(new Color(0, 175, 239));
		btnSubcategoria.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				panel.setVisible(false);
				TelaAplicacoes.AbrirsubCategoria();
			}
		});
		
		GroupLayout gl_utility = new GroupLayout(utility);
		gl_utility.setHorizontalGroup(
			gl_utility.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_utility.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_utility.createParallelGroup(Alignment.TRAILING)
						.addComponent(scrollPane_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
						.addComponent(categoria, GroupLayout.PREFERRED_SIZE, 155, Short.MAX_VALUE)
						.addComponent(excluir, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
						.addComponent(editar, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
						.addComponent(btnSubcategoria, GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
						.addComponent(Item, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_utility.setVerticalGroup(
			gl_utility.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_utility.createSequentialGroup()
					.addGap(5)
					.addComponent(categoria)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnSubcategoria)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(Item)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane_1, GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(editar)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(excluir))
		);
		utility.add(categorias = new JPanel());
		scrollPane_1.setViewportView(categorias);
		categorias.setForeground(new Color(255, 255, 255));
		//categorias.setBorder(BorderFactory.createTitledBorder("CATEGORIAS"));
		categorias.setBackground(new Color(0, 175, 239));
		categorias.setLayout(new GridLayout(1, 0, 0, 0));
		
		tree = new JTree();
		atualizarRaizEstoque();
		tree.setBackground(new Color(100, 175, 239));
		categorias.add(tree);
		utility.setLayout(gl_utility);
		
		estoque = new JPanel();
		estoque.setBackground(new Color(0, 175, 239));
		estoque.setLayout(new BorderLayout(0, 0));
		estoque.add(conteudo, BorderLayout.CENTER);
		conteudo.setLayout(new GridLayout(0, 1, 0, 0));
		
		scrollPane = new JScrollPane();
		scrollPane.getViewport().setBackground(new Color(100, 175, 239));
		table = new JTable();
		buscarTodos();
		

		
		scrollPane.setViewportView(table);
		conteudo.add(scrollPane);
		estoque.add(utility, BorderLayout.WEST);
		
		panel = new JPanel();
		panel.setVisible(true);
		panel.setBackground(null);
		estoque.add(panel, BorderLayout.NORTH);
		
		JLabel lblNewLabel = new JLabel("ID");
		
		tid = new JTextField();
		
		JLabel lblNome = new JLabel("NOME");
		
		tnome = new JTextField();
		
		JButton btnCoonsultar = new JButton("CONSULTAR");

		btnCoonsultar.setBackground(new Color(255, 255, 255));
		btnCoonsultar.setForeground(new Color(0, 175, 239));

		btnCoonsultar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				consultar();
			}
		});
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
					.addContainerGap(522, Short.MAX_VALUE)
					.addComponent(lblNewLabel)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(tid, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(lblNome)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(tnome, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnCoonsultar))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnCoonsultar)
						.addComponent(tnome, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNome)
						.addComponent(tid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		atualizarRaizEstoqueSub();
	}
	
	
	
	
	public static ArrayList<Sub_Categoria> BuscarSubCategoria(){
		   gapp = new GerenciadorAplicacoes();
	   		ArrayList<Sub_Categoria> lista = gapp.buscarSubCategoria();
	   		
	   		return lista;
	   	}
	
	
	   public static ArrayList<Categoria> BuscarCategoria(){
		   gapp = new GerenciadorAplicacoes();
	   		ArrayList<Categoria> lista = gapp.buscarCategoria();
	   		
	   		return lista;
	   	}

	public void atualizarRaizEstoque(){
		
		
		ArrayList<Categoria> al = BuscarCategoria();
		String[] dados = new String[al.size()]; 
		
		for (int i=0;i<al.size();i++){
			TelaCategoriaEstoque.c = al.get(i);
			dados[i] = TelaCategoriaEstoque.c.nome;
			
			cat = new DefaultMutableTreeNode(dados[i]);
			raiz.add(cat);
			
		}
		
		DefaultTreeModel mutable = new DefaultTreeModel(raiz);
		tree.setModel(mutable);
			
	}
	
	
	
	
	
	
	
	
public void atualizarRaizEstoqueSub(){
		
		ArrayList<Sub_Categoria> scal = BuscarSubCategoria();
		String[] subdados = new String[scal.size()]; 
		
		for (int i=0;i<scal.size();i++){
			Sub_Categoria sc = scal.get(i);
			subdados[i] = TelaCategoriaEstoque.c.nome;
			
			cat.add(subcategoria);
			
		}
		
	}
	
	
	
	
	
	

	
	public void buscarTodos(){
		ArrayList<Item> al = BuscarItens();
		AtualizarTabela(al);
	}
	
	public static ArrayList<Item> BuscarItens(){
		
		gapp = new GerenciadorAplicacoes();
		gapp.buscarItem();
		ArrayList<Item> lista = gapp.buscarItem();
		return lista;
	}
	
public void AtualizarTabela(ArrayList<Item> al){
		
		Object[][] dados = new Object[al.size()][5];

		for (int i=0;i<al.size();i++){
			
			Item it = al.get(i);
			
			dados[i][0] = it.id; 
			dados[i][1] = it.nome;
			dados[i][2] = it.qtd_total;
			dados[i][3] = it.qtd_usada;
			dados[i][4] = it.msg;
			}
		String[] nomesDasColunas = new String[] {"ID", "ITEM", "Quantidade TOTAL", "Quantidade USADO", "MENSAGENS ADICIONAIS"};
		
		DefaultTableModel tableModel = new DefaultTableModel(dados,nomesDasColunas);
		table.setModel(tableModel);
		
}

public void consultar(){
	ArrayList<Item> iTem = new ArrayList<Item>();
	Item it = new Item();
	
	it.nome = tnome.getText();
	String id = tid.getText();
	
    if (it.nome.equals("") && id.equals("")){
    	
		JOptionPane.showMessageDialog(null, "Informe um NOME ou un ID!");
	}else{
    
		if (id.equals("")){
			it.id = 0;
		}else{
			it.id = Integer.parseInt(tid.getText());
		}
		
    iTem = gapp.pesquisarItem(it);

	AtualizarTabela(iTem);
	
	for(int i = 0; i < iTem.size(); i++){
		table.setRowSelectionInterval(i, i);
	}}
    
	}

	
}
